package com.adroiti.texasholdem.engine;

import java.util.ArrayList;
import java.util.List;

import com.adroiti.texasholdem.Card;
import com.adroiti.texasholdem.Combination;
import com.adroiti.texasholdem.Hand;
import com.adroiti.texasholdem.Table;

public class PlayerHand implements Hand, Comparable<Hand> {

	private List<Card> cards = new ArrayList<Card>(2);
	private Combination combination;
	private Table table;
	
	public PlayerHand(Table table) {
		this.table = table;
	}
	
	@Override
	public Combination getCombination() {
		return combination;
	}

	@Override
	public int getCardsRank() {
		int num = 0;
		for (Card card : getCards()) {
			num += card.getRank();
		}
		return num;
	}

	@Override
	public List<Card> getCards() {
		return cards;
	}

	@Override
	public void addCard(Card card) {
		this.cards.add(card);
	}

	@Override
	public int compareTo(Hand o) {
		if(getCombination().getCombinationRank() > o.getCombination().getCombinationRank()){
			return -1;
		}
		else if(getCombination().getCombinationRank() < o.getCombination().getCombinationRank()){
			return 1;
		}
		else if(getCardsRank() > o.getCardsRank()){
			return -1;
		}
		else if(getCardsRank() < o.getCardsRank()){
			return 1;
		}
		else {
			return 0;
		}
			
	}

	@Override
	public void setCombination(Combination combination) {
		this.combination = combination;		
	}
	
	@Override
	public String toString(){
		StringBuilder builder = new StringBuilder();
				
		for(Card card : getCards()){
			builder.append(card.getColor().getTitleLetter());
			
			builder.append(card.getTitleLetter());
			builder.append(" ");
		}
		builder.append(": ");
		
		for(Card card : table.getCards()){
			builder.append(card.getColor().getTitleLetter());
			builder.append(card.getTitleLetter());
			builder.append(" ");
		}
		
		return builder.substring(0, builder.length() - 1);
	}

}
